/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gac.gacmod.stepmanager;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.ServiceLoader;
import org.gac.gacmod.model.Step;
import org.gac.gacmod.model.StepCat;

/**
 *
 * @author Tomek
 */
public class StepManager {

    private LinkedList<StepCategory> _categories = new LinkedList<StepCategory>();


    public static final StepManager Instance = new StepManager();
    private static final String _defaultStepCategory = "Genetic steps";
    private StepManager()
    {
        ServiceLoader<Step> loader = ServiceLoader.load(Step.class);

        HashMap<String,StepCategory> categories = new HashMap<String, StepCategory>();
        //StepCategory geneticCat = new StepCategory( "Genetic steps" );
        //categories.put("Genetic steps", genetic)

        for( Step s: loader)
        {
            StepCat sc = s.getClass().getAnnotation(StepCat.class);
            String scName = _defaultStepCategory;
            if( sc != null )
                scName = sc.value();
            if( !categories.containsKey(scName))
            {
                categories.put(scName, new StepCategory( scName));
            }
            categories.get(scName).add(new StepTypeWrapper( s.getClass()));
            //geneticCat.add(new StepTypeWrapper(s.getClass()));
        }
        for( StepCategory cat: categories.values())
        {
            _categories.add(cat);
        }
        //_categories.add(geneticCat);

//        StepCategory cat1 = new StepCategory( "Genetic steps" );
//        cat1.add(new StepTypeWrapper( ShowMessageStep.class ));
//        cat1.add(new StepTypeWrapper( DiagnosticsMessageStep.class ));
//        cat1.add(new StepTypeWrapper( CrossingOverStep.class ));
//        _categories.add( cat1 );
//
//        StepCategory cat2 = new StepCategory( "Flow steps" );
//        cat2.add(new StepTypeWrapper( SequenceStep.class ));
//        cat2.add(new StepTypeWrapper( SplitStep.class ));
//        cat2.add(new StepTypeWrapper( RepeatStep.class ));
//        _categories.add( cat2 );
    }

    public Collection<StepCategory> stepCategories()
    {
        return Collections.unmodifiableCollection(_categories);
    }
}
